#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "log.hpp"
#include <cstring>
#include <string>

using namespace LogModule;

int main()
{
    int sockfd = ::socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd < 0)
    {
        LOG(ERROR)<<"socket创建失败";
    }
    LOG(INFO)<<"socket创建成功";
    //填充服务器信息
    struct sockaddr_in sever;
    sever.sin_family = AF_INET;
    sever.sin_port = ::htons(8080);
    sever.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    while(true)
    {
        //发送消息
        std::cout<<"please enter:"<<std::endl;
        std::string meassage;
        std::getline(std::cin,meassage);
        ::sendto(sockfd,meassage.c_str(),meassage.size(),0,(sockaddr*)&sever,sizeof(sever));
        //接收消息
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        char buffer[1024];
        int n = ::recvfrom(sockfd,buffer,sizeof(buffer),0,(sockaddr*)&temp,&len);
        if(n>0)
        {
            buffer[n] = 0;
            std::cout<<buffer<<std::endl;
        }
    }

    return 0;
}